package vn.ikame.android.plugin;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.kxml2.io.KXmlParser;
import org.w3c.dom.Document;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class Parser {
	public static ArrayList<Comment> cmList;

	public static ArrayList<Comment> getCommentList(String url)
			throws Exception {
		ArrayList<Comment> qList = new ArrayList<Comment>();
		InputStreamReader is = null;

		try {
			URL myURL = new URL(url);

			HttpURLConnection conn = (HttpURLConnection) myURL.openConnection();
			conn.setConnectTimeout(10000);
			conn.setReadTimeout(10000);

			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse(myURL.openStream());

			DOMSource source = new DOMSource(document);
			StringWriter xmlAsWriter = new StringWriter();

			StreamResult result = new StreamResult(xmlAsWriter);

			TransformerFactory.newInstance().newTransformer()
					.transform(source, result);
			ByteArrayInputStream inputStream = new ByteArrayInputStream(
					xmlAsWriter.toString().getBytes("UTF-8"));

			is = new InputStreamReader(inputStream, "UTF-8");

		} catch (UnsupportedEncodingException ex) {
			ex.printStackTrace();
		}
		KXmlParser parser = new KXmlParser();
		int iCount = 0;
		try {

			parser.setInput(is);
			parser.nextTag();
			parser.require(XmlPullParser.START_TAG, null, "datas");

			String userName = "", comment = "";

			while (parser.next() != XmlPullParser.END_DOCUMENT) {
				if (parser.getEventType() == XmlPullParser.START_TAG) {
					String tag = parser.getName();

					if ("total".equals(tag)) {
						Comment.total = Integer.valueOf(parser.nextText()
								.trim());
					}
					if ("username".equals(tag)) {
						userName = parser.nextText().trim();
					} else if ("comment".equals(tag)) {
						comment = parser.nextText().trim();
					}

				} else if (parser.getEventType() == XmlPullParser.END_TAG) {
					String tag2 = parser.getName();
					if ("data".equals(tag2)) {
						Comment s = new Comment(userName, comment);
						qList.add(s);
						Log.w("HienPL", iCount + ": " + userName + " " + " "
								+ comment + " " + Comment.total);

						iCount++;
					}
				}
			}
		} catch (XmlPullParserException ex) {
			System.out.println("er1");
		} catch (IOException ex) {
			System.out.println("er2");
		}
		return qList;
	}

	public static INotification getNotification(INotification noti, String url)
			throws Exception {
		InputStreamReader is = null;

		try {
			URL myURL = new URL(url);

			HttpURLConnection conn = (HttpURLConnection) myURL.openConnection();
			conn.setConnectTimeout(10000);
			conn.setReadTimeout(10000);

			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse(myURL.openStream());

			DOMSource source = new DOMSource(document);
			StringWriter xmlAsWriter = new StringWriter();

			StreamResult result = new StreamResult(xmlAsWriter);

			TransformerFactory.newInstance().newTransformer()
					.transform(source, result);
			ByteArrayInputStream inputStream = new ByteArrayInputStream(
					xmlAsWriter.toString().getBytes("UTF-8"));

			is = new InputStreamReader(inputStream, "UTF-8");

		} catch (UnsupportedEncodingException ex) {
			ex.printStackTrace();
		}
		KXmlParser parser = new KXmlParser();
		try {

			parser.setInput(is);
			parser.nextTag();
			parser.require(XmlPullParser.START_TAG, null, "datas");

			String title = "", content = "", link = "", icon = "";
			int type = 0, time = 0;

			while (parser.next() != XmlPullParser.END_DOCUMENT) {
				if (parser.getEventType() == XmlPullParser.START_TAG) {
					String tag = parser.getName();

					if ("total".equals(tag)) {
						Comment.total = Integer.valueOf(parser.nextText()
								.trim());
					}
					if ("title".equals(tag)) {
						title = parser.nextText().trim();
						Log.d("HienPL", title);
						noti.setName(title);
					} else if ("content".equals(tag)) {
						content = parser.nextText().trim();
						Log.d("HienPL", content);
						noti.setContent(content);
					} else if ("link".equals(tag)) {
						link = parser.nextText().trim();
						Log.d("HienPL", link);
						noti.setAppLink(link);
					} else if ("icon".equals(tag)) {
						icon = parser.nextText().trim();
						Log.d("HienPL", icon);
						noti.setIconLink(icon);
					} else if ("type".equals(tag)) {
						type = Integer.parseInt(parser.nextText().trim());
						Log.d("HienPL", String.valueOf(type));
						noti.setType(type);
					} else if ("time".equals(tag)) {
						time = Integer.parseInt(parser.nextText().trim());
						Log.d("HienPL", String.valueOf(time));
						noti.setTimeDelay(time);
					}

				} else if (parser.getEventType() == XmlPullParser.END_TAG) {
					String tag2 = parser.getName();
					if ("data".equals(tag2)) {
						// noti = new INotification(icon, title, content, link,
						// type, time);
					}
				}
			}
		} catch (XmlPullParserException ex) {
			System.out.println("er1");
		} catch (IOException ex) {
			System.out.println("er2");
		}
		return noti;
	}

	public static boolean isNetworkConnected(Context ctx) {
		ConnectivityManager cm = (ConnectivityManager) ctx
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = cm.getActiveNetworkInfo();
		if (networkInfo == null) {
			return false;
		}
		return true;
	}
}
